<template>
	<view class="">
		<!-- 特殊类型跳转 -->
		<block v-if="visible">
			<!-- #ifdef H5 -->
			<view style="width: 100%;" v-html="wxOpenTags"></view>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<view class="launch-btn" @click="toMiniProgram(username,path)">

			</view>
			<!-- #endif -->
		</block>
	</view>
</template>

<script>
	// #ifdef H5
	import {
		Weixin
	} from 'common/js/wx-jssdk.js';
	// #endif
	// import jweixin from 'jweixin-module';
	export default {
		name: "toWeapp",
		props: {
			path: {
				type: String,
				default: ''
			},
			username: {
				type: String,
				default: ''
			},
			hasLogin: {
				type: Boolean,
				default: false
			},
			isLogin: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				wxOpenTags: '',
				visible: false
			};
		},
		methods: {
			jssdk_configs() {
				console.log('进来了');
				let _this = this;
				let url = uni.getSystemInfoSync().platform === 'ios' ? uni.getStorageSync('initUrl') : window.location.href;
				this.$api.sendRequest({
					url: '/wechat/api/wechat/jssdkconfig',
					data: {
						url: url
					},
					success: res => {
						if (res.code == 0) {
							console.log(res.data);
							wx.config({ // eslint-disable-line
								debug: false, // 调试，发布的时候改为false
								appId: res.data.appId,
								nonceStr: res.data.nonceStr,
								timestamp: res.data.timestamp,
								signature: res.data.signature,
								jsApiList: ['onMenuShareTimeline'], // 必填，需要使用的JS接口列表
								openTagList: ['wx-open-launch-weapp'] // 可选，需要使用的开放标签列表，例如['wx-open-launch-app']
							});
							wx.ready(function () {
								console.log('ready')
							});
						}
					}
				})
			},
			toMiniProgram(appId, path) {
				uni.navigateToMiniProgram({
					appId,
					path
				})
			}
		},
		created() {
			// #ifdef H5
			this.wxOpenTags = `<wx-open-launch-weapp
				id="launch-btn"
				style="position:absolute;top:0;left:0;width:100%;height:100%;display:inline-block;border:none;overflow:hidden;z-index:100"
				username="` + this.username + `" 
				path="` + this.path + `">
				<script type="text/wxtag-template">
					<div style="width: 1000px; height: 1000px;border:none;background-color: transparent"></div>
				<\/script>
			</wx-open-launch-weapp>`;

			const oScript = document.createElement('script');
			oScript.type = 'text/javascript';
			oScript.src = 'https://res2.wx.qq.com/open/js/jweixin-1.6.0.js';
			oScript.onload = this.jssdk_configs
			document.body.appendChild(oScript);
			// #endif
			if (!this.isLogin) {
				this.visible = true;
			}
		},
		watch: {
			hasLogin: {
				handler(newVal, oldVal) {
					if (newVal) {
						this.visible = true;
					}
				},
				immediate: true,
				deep: true
			}
		}
	}
</script>

<style lang="scss">
	.launch-btn {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		display: inline-block;
		border: none;
		overflow: hidden;
		z-index: 100
	}
</style>